home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / CD Audio Toolkit 1.0 / Source / FormatString.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  1.9 KB  |  104 lines  |  [TEXT/MPS ]

  1. #include <cd.h>
  2.  
  3. /************************************************************************
  4.  *
  5.  *  Function:        FormatString
  6.  *
  7.  *  Purpose:        prepare return string
  8.  *
  9.  *  Returns:        nothing
  10.  *
  11.  *  Side Effects:    creates C string from input parameters
  12.  *
  13.  *  Description:    For each of the numArgs values in num[], convert the 
  14.  *                    value to an ascii string and concatenate.
  15.  *
  16.  ************************************************************************/
  17. void
  18. FormatString(str, num, numArgs)
  19. char    str[];
  20. long     num[];
  21. long    numArgs;
  22. {
  23.     short    i;
  24.     char    numStr[31];
  25.     
  26.     ltoa(num[0], str);
  27.     for (i = 1; i < numArgs; i++)
  28.     {
  29.         strcat(str, ",");        /* add comma to separate items */
  30.         ltoa(num[i], numStr);
  31.         strcat(str, numStr);
  32.     }
  33.     c2pstr(str);    
  34. }
  35.  
  36.  
  37. /************************************************************************
  38.  *
  39.  *  Function:        ltoa
  40.  *
  41.  *  Purpose:        convert long to ascii
  42.  *
  43.  *  Returns:        nothing
  44.  *
  45.  *  Side Effects:    fills 's' with ascii representation of a
  46.  *
  47.  *  Description:    straight out of K&R, page 60. 
  48.  *
  49.  ************************************************************************/
  50. void
  51. ltoa(n, s)
  52. long    n;
  53. char    s[];
  54. {
  55.     int    i, sign;
  56.     
  57.     if ((sign = n) < 0)
  58.         n = -n;
  59.     
  60.     i = 0;
  61.     
  62.     do {
  63.         s[i++] = n % 10 + '0';    /* generate digits in reverse order */
  64.     } while ((n /= 10) > 0);    /* get rid of digit just generated */
  65.     
  66.     if (i == 1)
  67.         s[i++] = '0';            /* add preceeding zero */
  68.     if (sign < 0)
  69.         s[i++] = '-';
  70.     
  71.     s[i] = '\0';
  72.     
  73.     reverse(s);
  74. }
  75. /************************************************************************
  76.  *
  77.  *  Function:        reverse
  78.  *
  79.  *  Purpose:        reverse string s in place
  80.  *
  81.  *  Returns:        nothing
  82.  *
  83.  *  Side Effects:    string 's' is reversed.  The first char is last,
  84.  *                    the last is first.
  85.  *
  86.  *  Description:    straight from K&R, page 59
  87.  *
  88.  ************************************************************************/
  89. void
  90. reverse(s)
  91. char    s[];
  92. {
  93.     int    c, i, j;
  94.     
  95.     for (i = 0, j = strlen(s)-1; i < j; i++, j--)
  96.     {
  97.         c = s[i];
  98.         s[i] = s[j];
  99.         s[j] = c;
  100.     }
  101. }
  102.  
  103.  
  104.